<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2019/8/23
 * Time: 14:49
 */

namespace backend\controllers;

use backend\models\User;
use Yii;
use yii\data\Pagination;

class UserController extends BaseController
{
    public function actionIndex()
    {
        $pageSize = $this->pageSize;
        return $this->render('index',compact('pageSize'));
    }

    /**
     * 列表
     */
    public function actionList()
    {
        $page = intval(input('page'));
        $limit = intval(input('limit'));
        $data = User::find()->joinWith('role')
            ->offset(($page-1)*$limit)
            ->limit($limit)
            ->asArray()->all();
        echo json_encode(['code' => 0, 'msg' => 'success', 'count' => 2, 'data' => $data]);
        exit;
    }

    public function actionCreate()
    {
        return $this->render('create');
    }
    public function actionStatus()
    {
        $id = (int)input('id');
        $status = (int)input('status');
        $user = User::findOne($id);
        $user->status = $status==1?2:1;
        if($user->save()){
            $this->json(200,'状态更改成功');
        }
        $this->json(100,array_values($user->getFirstErrors())[0]);
    }

}
